﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
namespace LY.DataAccess.Repository
{
    public class SysTemplateRepository : BaseRepository
    {

        //查询出所有的模板信息
        public PagedList<S_SysTemplate> SysTemplateList(SysTemplateSearch search, PageView view)
        {
            string cols = @"[Id]
                      ,[TemplateName]
                      ,[TemplateCode]
                      ,[TemplateContent]
                      ,[TemplateType]";
            string sqlWhere = "";
            //if (!string.IsNullOrEmpty(search.LinkUrl))
            //{
            //    sqlWhere += " AND [LinkName] LIKE '%" + search.LinkUrl + "%' or LinkUrl like '%" + search.LinkUrl + "%'";
            //}
            return base.PageGet<S_SysTemplate>(view, cols, @" [S_SysTemplate] ", sqlWhere, "[Id] ASC", "");
        }
        //根据ID查询出一个系统模块的信息
        public S_SysTemplate GetSysTemplate(int? id)
        {
            string sql = @" SELECT [Id]
                      ,[TemplateName]
                      ,[TemplateCode]
                      ,[TemplateContent]
                      ,[TemplateType]
                           FROM [S_SysTemplate] WHERE Id=@ID";
            return base.Get<S_SysTemplate>(sql, new { ID = id });
        }

        //根据ID查询出一个系统模块的信息
        public S_SysTemplate GetSysTemplate(string code)
        {
            const string sql = @" SELECT [Id]
                      ,[TemplateName]
                      ,[TemplateCode]
                      ,[TemplateContent]
                      ,[TemplateType]
                           FROM [S_SysTemplate] WHERE TemplateCode=@TemplateCode";
            return base.Get<S_SysTemplate>(sql, new { TemplateCode = code });
        }

        //删除
        public int DeleteSysTemplate(int id)
        {
            string sql = "delete from S_SysTemplate where Id=@ID";
            return base.Excute(sql, new { ID = id });
        }

        //每个手机短信发送次数
        public int GetCodeCount(string phone, string ip)
        {
            string sql = @"SELECT COUNT(ID) AS [COUNT]
  FROM [T_Code] WHERE Tel=@Tel and CONVERT(char(10),[CreateDate],120)=CONVERT(char(10),GETDATE(),120)
  and [UserIp]=@UserIp";

            return base.Get<int>(sql, new { Tel = phone, UserIp=ip });
        }
    }
}
